package com.jingfeng.santiago.cnbeta;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import android.util.Xml;

public class XmlPullFeedParser {

    static final String CHANNEL = "channel";
    static final String PUB_DATE = "pubDate";
    static final String DESCRIPTION = "description";
    static final String LINK = "link";
    static final String TITLE = "title";
    static final String ITEM = "item";

    public ArrayList<FeedItem> parse(InputStream inputStream) {
        ArrayList<FeedItem> FeedItems = null;
        XmlPullParser parser = Xml.newPullParser();
        try {
            // auto-detect the encoding from the stream
            parser.setInput(inputStream, null);
            int eventType = parser.getEventType();
            FeedItem currentFeedItem = null;
            boolean done = false;
            while (eventType != XmlPullParser.END_DOCUMENT && !done) {
                String name = null;
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        FeedItems = new ArrayList<FeedItem>();
                        break;
                    case XmlPullParser.START_TAG:
                        name = parser.getName();
                        if (name.equalsIgnoreCase(ITEM)) {
                            currentFeedItem = new FeedItem();
                        } else if (currentFeedItem != null) {
                            if (name.equalsIgnoreCase(LINK)) {
                                currentFeedItem.setLink(parser.nextText());
                            } else if (name.equalsIgnoreCase(DESCRIPTION)) {
                                currentFeedItem.setDescription(parser.nextText());
                            } else if (name.equalsIgnoreCase(PUB_DATE)) {
                                currentFeedItem.setPubDate(parser.nextText());
                            } else if (name.equalsIgnoreCase(TITLE)) {
                                currentFeedItem.setTitle(parser.nextText());
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        name = parser.getName();
                        if (name.equalsIgnoreCase(ITEM) && currentFeedItem != null) {
                            FeedItems.add(currentFeedItem);
                        } else if (name.equalsIgnoreCase(CHANNEL)) {
                            done = true;
                        }
                        break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
//			Log.e("AndroidNews::PullFeedParser", e.getFeedItem(), e);
            throw new RuntimeException(e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return FeedItems;
    }
}
